package com.electric.qrapp.ui.base.frg

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/*
 *@author created by WA03675
 *@date  2021-08-02 13:39
 *@des
 */
abstract class BaseFragment<T : ViewDataBinding> : SimpleFragment() {

    protected lateinit var dataBinding: T

    private var lastView: View? = null //记录上一次创建的视图
    private var isInit:Boolean = false //是否已经初始化

    override fun defaultInflater(inflater: LayoutInflater, container: ViewGroup?): View {
        dataBinding = DataBindingUtil.inflate(layoutInflater, getLayoutId(), container, false)
        dataBinding.lifecycleOwner = this
        return dataBinding.root
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (lastView == null) {
            lastView = super.onCreateView(inflater, container, savedInstanceState)
        }
        return lastView
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (!isInit) {
            initBinding()
            super.onViewCreated(view, savedInstanceState)
            isInit = true
        }
    }


    //绑定DataBinding
    abstract fun initBinding()


}